"""
日志配置模块
"""
import logging
from typing import Optional


class Logger:
    """日志管理类"""

    def __init__(self, name: str = "novel_scraper"):
        self.logger = logging.getLogger(name)
        self._setup_logger()

    def _setup_logger(self):
        """配置日志"""
        if not self.logger.handlers:
            handler = logging.StreamHandler()
            formatter = logging.Formatter(
                '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
            )
            handler.setFormatter(formatter)
            self.logger.addHandler(handler)
            self.logger.setLevel(logging.INFO)

    def info(self, message: str, site: Optional[str] = None):
        """记录信息日志"""
        if site:
            message = f"[{site}] {message}"
        self.logger.info(message)

    def error(self, message: str, site: Optional[str] = None):
        """记录错误日志"""
        if site:
            message = f"[{site}] {message}"
        self.logger.error(message)

    def warning(self, message: str, site: Optional[str] = None):
        """记录警告日志"""
        if site:
            message = f"[{site}] {message}"
        self.logger.warning(message)


# 全局日志实例
logger = Logger()